//
//  WmcVideoProgressView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/5/19.
//

import UIKit
import YPBedrock

class WmcVideoControlBottomView: WmcBaseView {
    lazy var playPauseBtn: UIButton = {
        let btn = UIButton()
        btn.setImage("player_pause_icon".wmc_image, for: .selected)
        btn.setImage("player_play_icon".wmc_image, for: .normal)
        return btn
    }()

    lazy var timeLabel: UILabel = {
        let label = UILabel.YP.Create(text: "-/-",
                                      font: 14,
                                      color: UIColor.white,
                                      alignment: .right)
        return label
    }()

    lazy var progressView: UIView = {
        let view = UIView()
        view.backgroundColor = .wmc_mainTheme
        return view
    }()

    lazy var progressBackgroundView: UIView = {
        let view = UIView()
        view.backgroundColor = .white.yp.alpha(0.2)
        view.layer.cornerRadius = 1
        view.layer.masksToBounds = true
        return view
    }()
    
    lazy var sliderBlockView: UIView = {
        let view = UIView()
        view.isUserInteractionEnabled = true
        return view
    }()
    
    lazy var sliderView: UIView = {
        let view = UIView()
        view.isUserInteractionEnabled = true
        view.backgroundColor = .white
        view.layer.shadowColor = UIColor.black.cgColor
        view.layer.shadowOpacity = 0.47
        view.layer.shadowOffset = .zero
        view.layer.cornerRadius = 8
        return view
    }()
    

    /// 拖拽
    lazy var panGesture: UIPanGestureRecognizer = {
        let gesture = UIPanGestureRecognizer.YP.Create { [weak self] pan in
            guard let weakSelf = self else { return }
            let translation = pan.translation(in: weakSelf.sliderBlockView)
            guard let panView = pan.view else { return }
            pan.setTranslation(CGPoint.zero, in: weakSelf.sliderBlockView) // 归零
            var x = panView.center.x + translation.x
            let minX = weakSelf.progressBackgroundView.frame.origin.x
            if x < minX {
                x = minX
            }
            let maxX = weakSelf.progressBackgroundView.frame.origin.x + weakSelf.progressBackgroundView.frame.width
            if x > maxX {
                x = maxX
            }
            let progress = (x - minX) / weakSelf.progressBackgroundView.frame.width
            let currentTime = weakSelf.totalTime * progress
            weakSelf.setProgress(currentTime: currentTime)
            switch pan.state {
            case .began:
                YPDevice.PlayShake(.heavy)
                weakSelf.timeBeganChangeBlock?()
            case .changed:
                weakSelf.timeChangeBlock?(currentTime)
            case .ended:
                YPDevice.PlayShake(.medium)
                weakSelf.timeEndedChangeBlock?()
            default: break
            }
        }
        return gesture
    }()

    var timeBeganChangeBlock: YPBlock.Empty?
    
    var timeEndedChangeBlock: YPBlock.Empty?

    /// 值变化
    var timeChangeBlock: YPBlock.OneParam<Double>?

    /// 总时间
    var totalTime: Double = 0

    override func makeUI() {
        isUserInteractionEnabled = true
        backgroundColor = .clear
        
        
        addSubview(playPauseBtn)
        playPauseBtn.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.centerY.equalToSuperview()
            make.size.equalTo(20)
        }
        
        addSubview(timeLabel)
        timeLabel.snp.makeConstraints { make in
            make.top.bottom.equalTo(playPauseBtn)
            make.right.equalTo(-16)
            make.width.equalTo(90)
        }
        
        addSubview(progressBackgroundView)
        progressBackgroundView.snp.makeConstraints { make in
            make.centerY.equalTo(playPauseBtn)
            make.left.equalTo(playPauseBtn.snp.right).offset(10)
            make.right.equalTo(timeLabel.snp.left).offset(-8)
            make.height.equalTo(2)
        }

        progressBackgroundView.addSubview(progressView)
        progressView.snp.makeConstraints { make in
            make.top.left.bottom.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(0)
        }
        addSubview(sliderBlockView)
        sliderBlockView.snp.makeConstraints { make in
            make.centerY.equalTo(progressBackgroundView)
            make.size.equalTo(44)
            make.centerX.equalTo(progressBackgroundView.snp.left).offset(0)
        }
        sliderBlockView.addGestureRecognizer(panGesture)

        sliderBlockView.addSubview(sliderView)
        sliderView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.size.equalTo(16)
        }
    }

    func setIsPlay(_ isPlay: Bool) {
        playPauseBtn.isSelected = isPlay
        if isPlay {
        } else {}
    }

    func setProgress(currentTime: Double) {
        // 换算总时间
        let currentTimeText = currentTime.wmc_secondsToTime
        let totalTimeText = totalTime.wmc_secondsToTime
        timeLabel.text = "\(currentTimeText) / \(totalTimeText)"

        var progress = currentTime / totalTime
        if progress < 0 || progress.isNaN {
            progress = 0
        }
        progressView.snp.remakeConstraints { make in
            make.top.left.bottom.equalToSuperview()
            make.width.equalToSuperview().multipliedBy(progress)
        }
        let progressWidth = progressBackgroundView.frame.width * progress
        sliderBlockView.snp.updateConstraints { make in
            make.centerX.equalTo(progressBackgroundView.snp.left).offset(progressWidth)
        }
    }
}

